package regpang.listviewtest.DragRecycler;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import regpang.listviewtest.R;

/**
 * 可拖拽式
 */

public class DragRecyclerAdapter extends RecyclerView.Adapter<DragRecyclerAdapter.ViewHolder> implements ItemDragHelperCallback.OnItemMoveListener {

    private List<Subject> datas;
    private Context mContext;
    private LayoutInflater mLiLayoutInflater;
    private List<Integer> indexList = new ArrayList<>();

    public DragRecyclerAdapter(List<Subject> datas, Context context) {
        this.datas = datas;
        this.mContext = context;
        this.mLiLayoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public DragRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLiLayoutInflater.inflate(R.layout.item_content, parent, false));
    }

    @Override
    public void onBindViewHolder(final DragRecyclerAdapter.ViewHolder holder, final int position) {
        //是否复用item
        holder.setIsRecyclable(true);
        final Subject subject = datas.get(position);
        holder.tv_title.setText(subject.getTitle());
        holder.img.setImageResource(subject.getImg());

        //记录改变
        if (subject != null) {
            holder.checkBox.setChecked(subject.isCheck());
        }

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                subject.setCheck(holder.checkBox.isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        //移动中
        Collections.swap(datas, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onSwiped(int fromPosition, int toPosition) {
        //交换
        Collections.swap(datas, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements OnDragVHListener {
        TextView tv_title;
        ImageView img;
        CheckBox checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.textView);
            img = (ImageView) itemView.findViewById(R.id.imageView);
            checkBox = itemView.findViewById(R.id.check_box);
        }

        @Override
        public void onItemSelected() {
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemFinish() {
            itemView.setBackgroundColor(Color.parseColor("#ff33b5e5"));
        }
    }
}
